package net.adetech.crj.web;


import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.Appointment;
import net.adetech.crj.entity.Hall;
import net.adetech.crj.entity.User;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
public class AppointmentWebController {

    @Autowired
    ServiceUtil serviceUtil;


    /**
     * 添加预约页面
     * @return
     */
    @RequestMapping("/appointment/add/page")
    public String addAppointment(String applyId, Model model, HttpSession session)
    {

        User user = (User)session.getAttribute("currentUser");
        if (applyId!=null)
        {
            ServiceResponse response = serviceUtil.callService("apply.getApplyById","applyId",applyId);
            Apply apply = (Apply) response.getResults().get("apply");
            if (apply.getApplyUserId().equals(user.getIdNumber()))
            {
                model.addAttribute("apply",apply);
            }
        }


        ServiceResponse response = serviceUtil.callService("hall.getAll");
        List<Hall> hallList = (List<Hall>)response.get("hallList");
        String paramsJson = TemplateUtil.buildJsonParams("allHallList",hallList);

        model.addAttribute("params",paramsJson);

        return  "addAppointment";
    }


    @RequestMapping("/appointment/group/add/page")
    public String addGroupAppointment(Model model)
    {
        ServiceResponse response = serviceUtil.callService("hall.getAll");
        List<Hall> hallList = (List<Hall>)response.get("hallList");
        String paramsJson = TemplateUtil.buildJsonParams("allHallList",hallList);
        model.addAttribute("params",paramsJson);
        return  "addGroupAppointment";
    }

    @RequestMapping("/appointment/view/page")
    public String viewAppointment(String appointmentId, Model model, HttpSession session)
    {

        User user = (User)session.getAttribute("currentUser");
        if (appointmentId!=null)
        {
            ServiceResponse response = serviceUtil.callService("appointment.getById","appointmentId",appointmentId);
            Appointment appointment = (Appointment) response.getResults().get("appointment");
            model.addAttribute("appointment",appointment);
            String params = "var appId = '"+appointmentId+"';";
            model.addAttribute("params",params);

        }

        return  "viewAppointment";
    }

    /**
     * 添加预约页面
     * @return
     */
    @RequestMapping("/appointment/list/page")
    public String appointmentList(HttpSession session)
    {
        return  "appointmentList";
    }




}
